﻿#include "Application.h"
#include "CreateMeshAction.h"
#include "CreateSystemAction.h"

Application::Application(int argc, char** argv)
  : _cl(argc, argv)
{
}

Application::~Application() {}

void Application::Run()
{
  ParseCLI(); //解析命令行

  PrintLogo(); // 打印logo

  ParseInputFile(); // 解析配置文件
};

void Application::PrintLogo()
{
  auto logo = R"(
                                                                                       
   @@@@@@@@@     .@@.     =@@@@@@]        @@`      @\       @ 
         =@`     @/=@     =@     \@`     //=@      @\@.     @ 
        /@.     =@  @\    =@     =@^    =@. \\     @ ,@`    @ 
      ,@/      =@`  ,@^   =@   ,/@/    ,@`   @^    @  .@\   @ 
     /@`      ,@\]]]]/@`  =@[[\@`      @\]]]]/@`   @    \@. @ 
   ,@/        @/      \@  =@   ,@^    /@      =@.  @     =@`@ 
  =@\]]]]]]] /@        @\ =@    .@\  =@.       @\  @      ,@@ 
  ---------------------------------------------------------------

)";
  std::cout << logo;
}

void Application::ParseCLI()
{
  _ifile = _cl.Follow("-i");

  if (_cl.Argc() == 1 || _cl.Get().search(2, "-h", "--help") || _ifile.empty())
  {
    _cl.PrintUsage();
    std::exit(0);
  }
}

void Application::ParseInputFile()
{
  _parser = std::make_shared<Parser>(_ifile);
  _awh.push_back(std::make_shared<CreateMeshAction>(*this));
  _awh.push_back(std::make_shared<CreateSystemAction>(*this));
  for (auto& action : _awh)
  {
    action->Execute();
  }
  //CreateMesh();
  CreateSystem();
  _sys->InitCondition();

  try
  {
    _sys->Solve();
  }
  catch (const std::exception& e)
  {
    std::cout << e.what() << std::endl;
  }

  //_sys->Output();

  vtkm::cont::Timer timer;
  timer.Start();
  ImgOutput img_output{ *_sys };
  img_output.Execute();
  Success("图片输出完成，耗时：", timer.GetElapsedTime(), "s");

  timer.Start();
  VTKOutput vtk_output{ *_sys };
  vtk_output.Execute();
  Success("后处理文件输出完成，耗时：", timer.GetElapsedTime(), "s");
}
